import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import CustomDialog from "@/components/HntManage/pouringOrder/WBS/index.vue";


import {
    saveOnSiteTestRequest, //保存
    getOnSiteRequestByFid, //详情

} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "info"

    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            continuity: true,//连续新增是否展示            
            dialogHeight: null,
            treeData: [], //材料种类
            formData: {},
            formObj: formJs.formObj,
            formBcObj: formJs.formBcObj,
            btnShow: true,
            currentDetail: null,
            fileList: [],
            tableDataInfo: [],
            options: [{
                value: '有',
                label: '有'
            }, {
                value: '无',
                label: '无'
            },],
            isEdit: false,
            infoGetList: null, //列表页getList

            WBSRow: {},
            WBSShow: false, //wbs弹框是否显示

        };
    },
    components: {
        publicForm,
        CustomDialog
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        }, 1)
                        this.continuity = false;
                    } else {
                        this.continuity = true;
                    }
                }
            },
            immediate: true,
        },
        info: {
            handler(newVal) {
                if (newVal) {
                    this.infoGetList = newVal;
                }
            },
            immediate: true,
        }
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.btnShow = true;
        this.formObj.map((item) => {
            item.disabled = true;
        });
        this.formBcObj.map((item) => {
            item.disabled = false;
        });
        this.formBcObj[2].inputButton = true;

        this.isEdit = false;
        if (this.statusNum == 1) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.getDetail({
                fId: null, // 数据fid 查详情时传值, 预新增时传null
                fCompanyid: this.infoGetList.fCompanyid // 当前标段id,预新增时传,用来查询编号
            }, 0)

        } else if (this.statusNum == 2) { //编辑
            this.formData.fId = this.currentDetail.fId;//按钮 
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false;
            this.isEdit = true;
            this.formBcObj.map((item) => {
                item.disabled = true;
            });
            this.formBcObj[2].inputButton = false;
        }
        this.getHeight();
    },
    methods: {
        //  wbs
        refreshList() { },

        getWBS(data) {
            this.formData.fWbsname = data.fLongname; //施工部位
            this.formData.fWbsid = data.fLongids //施工部位id
        },
        WBSDialog() {
            this.WBSShow = false;
        },
        clickDialog(index, prop) {
            if (prop == "fWbsname") {
                this.WBSShow = true
            }
        },
        //  wbs
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },

        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            // console.log(data, a, b);
            if (b == "fProjecttype") {
                this.formData.fCheckparam = "";
                this.formBcObj[1].options = [];
                if (data == "路基工程") {
                    this.formBcObj[1].options = formJs.fProjecttypeLJ;
                } else if (data == "桥梁工程") {
                    this.formBcObj[1].options = formJs.fProjecttypeQL;
                } else if (data == "钢结构") {
                    this.formBcObj[1].options = formJs.fProjecttypeGJG;
                } else if (data == "隧道工程") {
                    this.formBcObj[1].options = formJs.fProjecttypeSD;
                } else if (data == "路面工程") {
                    this.formBcObj[1].options = formJs.fProjecttypeLM;
                } else if (data == "==请选择==") {
                    this.formData.fProjecttype = "";
                }
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        // 详情
        getDetail(obj, num) {
            console.log("详情",obj, num);
            this.loading = true;
            getOnSiteRequestByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data;
                    this.loading = false;
                    if (num == 0) { //新增
                        console.log("lalalala");
                        this.formData = data;
                        this.formData.fRequestdates = this.common.day(0);
                        this.formData.fBuilddates = this.common.day(0);
                        this.formData.fSelftestdates = this.common.day(0);
                    } else if (num == 2) {
                        this.formData.fRequestnumber = data.fRequestnumber;//审检单号

                        this.formData.fProjectname = data.fProjectname;//建设单位
                        this.formData.fProjectid = data.fProjectid;//建设单位

                        this.formData.fJlname = data.fJlname;//监理单位
                        this.formData.fJlid = data.fJlid;//监理单位

                        this.formData.fCompanyname = data.fCompanyname;//施工单位
                        this.formData.fCompanyid = data.fCompanyid;//施工单位
                    } else {
                        this.formData = data;
                        this.tableDataInfo = data.detailList;
                    }
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_saveForm(status) {
            this.formData.fCreateuserid = this.userInfo.userId;
            this.formData.fCreateusername = this.userInfo.realName;
            this.formData.detailList = this.tableDataInfo;
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data, status) {
            this.loading = true;
            this.isLock = true;

            saveOnSiteTestRequest(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.isLock = false;
                        this.loading = false;
                        this.$message.success("新增成功");
                        if (status == 1) {
                            this.visible = false;
                            this.$emit("refreshList", status);
                        } else {
                            this.formData.fWbsname = ""; //施工部位
                            this.formData.fWbsid = "";//施工部位id
                            this.formData.fStructuralpart = "";//具体部位
                            this.getDetail({
                                fId: null, // 数据fid 查详情时传值, 预新增时传null
                                fCompanyid: this.infoGetList.fCompanyid // 当前标段id,预新增时传,用来查询编号
                            }, 2)
                        }
                    } else if (res.errcode == -1) {
                        this.loading = false;
                        this.visible = false;
                        this.$message.error(res.data);
                        this.$emit("refreshList", status);
                        this.isLock = false;
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                    }
                })
        },



        // // 存放位置
        // addUnqualifiedInfo() {
        //     this.tableDataInfo.push({
        //         fPilenumber: "", // 桩号
        //         fPilediameter: "", // 桩径(m)
        //         fDesignpiletopelevation: "", // 设计桩顶标高(m)
        //         fActualpiletopelevation: "", // 实测桩顶标高(m)
        //         fPipetopelevation: "", // 管顶标高(m)
        //         fDesignpilebottomelevation: "", // 设计桩底标高(m)
        //         fDesignpilelength: "", //设计桩长(m)
        //         fConstructionpilelength: "", // 施工桩长(m)
        //         fConstructiondates: this.common.day(0), //施工日期
        //         fIsexception: "无", // 有无异常
        //         fNote: "", // 备注
        //     });
        // },
        // changeText(row, index) {
        //     this.tableDataInfo[index] = JSON.parse(JSON.stringify(row));
        // },
        // // 删除存放位置
        // deleteUnqualifiedInfo(index) {
        //     this.tableDataInfo.splice(index, 1);
        // },
        // copyUnqualifiedInfo(index, row) {
        //     this.tableDataInfo.push({
        //         fPilenumber: row.fPilenumber, // 桩号
        //         fPilediameter: row.fPilediameter, // 桩径(m)
        //         fDesignpiletopelevation: row.fDesignpiletopelevation, // 设计桩顶标高(m)
        //         fActualpiletopelevation: row.fActualpiletopelevation, // 实测桩顶标高(m)
        //         fPipetopelevation: row.fPipetopelevation, // 管顶标高(m)
        //         fDesignpilebottomelevation: row.fDesignpilebottomelevation, // 设计桩底标高(m)
        //         fDesignpilelength: row.fDesignpilelength, //设计桩长(m)
        //         fConstructionpilelength: row.fConstructionpilelength, // 施工桩长(m)
        //         fConstructiondates: row.fConstructiondates, //施工日期
        //         fIsexception: row.fIsexception, // 有无异常
        //         fNote: row.fNote, // 备注
        //     })
        //     // console.log(index, row);
        // },
    },
};